package com.css.fxfzxczh.modules.actionTrack.repository.impl;


import com.css.fxfzxczh.constants.XczhConstants;
import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.actionTrack.entity.ActionTrackEntity;
import com.css.fxfzxczh.modules.actionTrack.repository.ActionTrackRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/27 10:19
 **/
@Repository
public class ActionTrackRepositoryImpl implements ActionTrackRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(ActionTrackEntity actionTrackEntity) {
        String sql="insert into xczh_action_track values(:footId, :userId, :longitude, :latitude, :remark, :createTime, :xsemId, :phasePoint)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(actionTrackEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }


    @Override
    public List<ActionTrackEntity> getActionTracks(String userId,String nowTime,String xsemId) {
       String sql="select *,st_astext(geom) geom from xczh_action_track where user_id= ? and create_time <= ? and xsem_id= ? ORDER BY create_time ASC";
        List<ActionTrackEntity> actionTrack = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ActionTrackEntity.class),userId,Timestamp.valueOf(nowTime),xsemId);
        return actionTrack;
    }

    @Override
    public ActionTrackEntity fingByFootId(String footId) {
        String sql="select * from xczh_action_track where foot_id = ?";
        ActionTrackEntity actionTrack = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ActionTrackEntity.class),footId);
        return actionTrack;
    }

    @Override
    public List<ActionTrackEntity> getAllPersonnelPosition(String xsemId) {
        String sql="select *,st_astext(geom) geom from xczh_action_track where phase_point = ? and xsem_id= ?";
        List<ActionTrackEntity> actionTrack = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ActionTrackEntity.class), XczhConstants.END_POINT,xsemId);
        return actionTrack;
    }

    @Override
    public List<ActionTrackEntity> getLeaderPosition(String xsemId) {
        String sql="select distinct(a.*),st_astext(geom) geom from xczh_action_track a left join xczh_investigation_team b on a.user_id=b.leader_id " +
                "where b.del_flag='"+ YNEnum.N +"' and a.phase_point = '"+XczhConstants.END_POINT+"' and a.xsem_id='"+xsemId+"' " +
                "and b.xsem_id='"+xsemId+"'";
        List<ActionTrackEntity> actionTrack = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ActionTrackEntity.class));
        return actionTrack;
    }

    @Override
    public String getLastFoot(String userId, String endPoint,String xsemId) {
        String sql = "select user_id  userId from xczh_action_track where  user_id = ? and phase_point = ? and xsem_id= ? ";
        try {
            String value = jdbcTemplate.queryForObject(sql, String.class,userId,endPoint,xsemId);
            return value;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public void deleteByUserId(String userId) {
        String sql = "delete from  xczh_action_track  where user_id= '"+userId+"'" ;
        jdbcTemplate.update(sql);
    }
}
